package com.gopay.webtest.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.Trans00700SignHandler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.MarketDepositApplyVO;
import com.gopay.webtest.xml.XmlDao;

/**
 * 市场入金Controller
 * 
 * @MarketDepositController.java
 * @author fanghw
 * @2013-12-26 上午10:58:11 www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/marketdeposit")
@Controller
public class MarketDepositController extends BaseController {
    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "trans00700SignHandler")
    private Trans00700SignHandler trans00700SignHandler;

    /**
     * 跳转到申请页面
     * 
     * @return
     */
    @RequestMapping("/apply")
    public ModelAndView apply() {
        ModelAndView mv = new ModelAndView();

        try {
            Map<String, String> map = xmlDao.readData4Map("marketdeposit_apply", "apply");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("marketdeposit.apply");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到申请页面", e);
        }

        return mv;
    }

    /**
     * 执行申请
     * 
     * @return
     */
    @RequestMapping("/doApply")
    public ModelAndView doApply(@ModelAttribute MarketDepositApplyVO applyVo) {
        ModelAndView mv = new ModelAndView();

        try {
            
            if("2.1".compareTo(applyVo.getVersion())<=0){
                //防钓鱼机制-国付宝服务器时间
                applyVo.setGopayServerTime(DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            // 签名
            trans00700SignHandler.sign(applyVo.getTranCode()+"_"+applyVo.getVersion(), "signType", "signValue", applyVo, getRequest());

            mv.setViewName("marketdeposit.doApply");
            mv.addObject("applyVo", applyVo);
        } catch (Exception e) {
            logger.error("执行申请", e);
        }

        return mv;
    }

}
